home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / uhren & terminkalender / organizer / stickit2 / source / commod.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  10KB  |  546 lines

  1. /**********************************************
  2.  **************   commod.c   ******************
  3.  **********************************************/
  4.  
  5. #define INTUI_V36_NAMES_ONLY
  6.  
  7. #include <libraries/commodities.h>
  8. #include <exec/exec.h>
  9. #include <intuition/intuition.h>
  10.  
  11. #include <clib/commodities_protos.h>
  12. #include <clib/exec_protos.h>
  13. #include <clib/gadtools_protos.h>
  14. #include <clib/intuition_protos.h>
  15.  
  16. #include <stdio.h>    /* For printf calls, remove when finished */
  17.  
  18. #include "stickit2.h"
  19.  
  20. #include "consts.h"
  21. #include "structs.h"
  22. #include "proto.h"
  23.  
  24. #define EVT_HOTKEY        1L
  25.  
  26. extern prj_p prj;
  27.  
  28. #ifdef _DCC
  29. extern __chip UWORD WaitPointer[];
  30. #else
  31. extern UWORD __chip WaitPointer[];
  32. #endif
  33.  
  34. /*
  35. Function : void commod_startup(void)
  36. Purpose : Sets up the program as a commodity.
  37. */
  38.  
  39. void commod_startup(void)
  40. {
  41.     struct NewBroker newbroker = {
  42.         NB_VERSION,
  43.         "StickIt2",
  44.         "StickIt2",
  45.         "Sticky Note Reminder - © Andy Dean 1993",
  46.         0,
  47.         COF_SHOW_HIDE,
  48.         0,
  49.         0,
  50.         0
  51.     };
  52.  
  53.     newbroker.nb_Port = prj->broker_msg_port;
  54.     newbroker.nb_Pri = prj->prefs.cx_priority;
  55.  
  56.     /* Set up main broker */
  57.  
  58.     prj->broker = CxBroker(&newbroker,NULL);
  59.  
  60.     if (!prj->broker)
  61.         error("Can't define broker",ERR_FATAL,__LINE__,__FILE__);
  62.  
  63.     /* Set up filter */
  64.  
  65.     prj->filter = CxFilter(prj->prefs.cx_popkey);
  66.  
  67.     if (!prj->filter)
  68.         error("Can't define filter",ERR_FATAL,__LINE__,__FILE__);
  69.  
  70.     /* Add filter to broker's personal list */
  71.  
  72.     AttachCxObj(prj->broker,prj->filter);
  73.  
  74.     /* Set up sender (wow this is tedious) */
  75.  
  76.     prj->sender = CxSender(prj->broker_msg_port,EVT_HOTKEY);
  77.  
  78.     if (!prj->sender)
  79.         error("Can't define sender",ERR_FATAL,__LINE__,__FILE__);
  80.  
  81.     AttachCxObj(prj->filter,prj->sender);
  82.  
  83.     /* Finally, the translater */
  84.  
  85.     prj->translate = CxTranslate(NULL);
  86.  
  87.     if (!prj->translate)
  88.         error("Can't define translate",ERR_FATAL,__LINE__,__FILE__);
  89.  
  90.     AttachCxObj(prj->filter,prj->translate);
  91.  
  92.     /* Test the commodities error of the whole object */
  93.  
  94.     if (CxObjError(prj->filter))
  95.         error("Can't build commodities object",ERR_FATAL,__LINE__,
  96. __FILE__);
  97.  
  98.     /* Activate the object (YEAH!) */
  99.  
  100.     ActivateCxObj(prj->broker,1L);
  101. }
  102.  
  103. /*
  104. Function : BOOL commod_event()
  105. Purpose : Handles a commodity event. Returns TRUE if a CXCMD_KILL is received,
  106.     else FALSE.
  107. */
  108.  
  109. BOOL commod_event()
  110. {
  111.     struct EasyStruct quitreq = {
  112.         sizeof(struct EasyStruct),
  113.         0,
  114.         "StickIt2",
  115.         "Data has changed, continue with quit ?",
  116.         "Okay|Cancel"
  117.     };
  118.  
  119.     CxMsg *cxmsg;
  120.  
  121.     ULONG msg_type,msg_id;
  122.     BOOL done = FALSE;
  123.  
  124.     /* Get message from port */
  125.  
  126.     while (cxmsg = (CxMsg *)GetMsg(prj->broker_msg_port)) {
  127.         msg_id = CxMsgID(cxmsg);
  128.         msg_type = CxMsgType(cxmsg);
  129.         ReplyMsg((struct Message *)cxmsg);
  130.  
  131.         switch (msg_type) {
  132.             case CXM_IEVENT:
  133.                 switch(msg_id) {
  134.                     case EVT_HOTKEY:
  135.                         openwindowcommod();
  136.                         break;
  137.                     default:
  138.                         break;
  139.                 }
  140.                 break;
  141.             case CXM_COMMAND:
  142.                 switch (msg_id) {
  143.                     case CXCMD_APPEAR:
  144.                         /* Who cares if it opens! */
  145.                         openwindowcommod();
  146.                         break;
  147.  
  148.                     case CXCMD_DISAPPEAR:
  149.                         /* Will only close if open */
  150.                         closewindowcommod();
  151.                         break;
  152.  
  153.                     case CXCMD_DISABLE:
  154.                         ActivateCxObj(prj->broker,0L);
  155.                         break;
  156.  
  157.                     case CXCMD_ENABLE:
  158.                         ActivateCxObj(prj->broker,1L);
  159.                         break;
  160.  
  161.                     case CXCMD_KILL:
  162.                         if (prj->projectchanged) {
  163.                             if (EasyRequest(
  164. commod,&quitreq,NULL,TAG_END))
  165.                                 done = TRUE;
  166.                         }
  167.                         else
  168.                             done = TRUE;
  169.  
  170.                         break;
  171.  
  172.                     default:
  173.                         break;
  174.                 }
  175.             default:
  176.                 break;
  177.         }
  178.     }
  179.  
  180.     return(done);
  181. }
  182.  
  183. /*
  184. Function : BOOL commodwin_event(struct IntuiMessage *curr_msg)
  185. Purpose : Handles a commodity window event. Returns TRUE if a user selects
  186.     Quit from the menus, else FALSE.
  187. */
  188.  
  189. BOOL commodwin_event(struct IntuiMessage *curr_msg)
  190. {
  191.     BOOL done = FALSE;
  192.  
  193.     done = processwindowcommod(curr_msg);
  194.  
  195.     return (done);
  196. }
  197.  
  198. /*
  199. Function : BOOL processwindowcommod(struct IntuiMessage *curr_msg)
  200. Purpose : A clone of Designer's event handler. The function returns TRUE if the
  201.     user selects Quit, else returns FALSE. If curr_msg->Class ==
  202.     IDCMP_CLOSEWINDOW, the window is closed here.
  203. */
  204.  
  205. BOOL processwindowcommod(struct IntuiMessage *curr_msg)
  206. {
  207.     BOOL done = FALSE;
  208.  
  209.     switch (curr_msg->Class) {
  210.         case IDCMP_REFRESHWINDOW:
  211.             GT_BeginRefresh(commod);
  212.             GT_EndRefresh(commod,TRUE);
  213.             break;
  214.         case IDCMP_CLOSEWINDOW:
  215.             done = commodclose();
  216.             break;
  217.         case IDCMP_MENUPICK:
  218.             done = processwindowmenucommod(curr_msg);
  219.             break;
  220.         case IDCMP_GADGETUP:
  221.             processwindowgadcommod(curr_msg);
  222.             break;
  223.         case IDCMP_VANILLAKEY:
  224.             /* Don't need a curr_note for "Add" */
  225.  
  226.             switch (curr_msg->Code) {
  227.                 case 'N':
  228.                 case 'n':
  229.                     commodnew();
  230.                     break;
  231.                 default:
  232.                     break;
  233.             }
  234.  
  235.             if (!prj->curr_note)
  236.                 break;
  237.  
  238.             switch (curr_msg->Code) {
  239.                 case 'L':
  240.                 case 'l':
  241.                     commoddel();
  242.                     break;
  243.                 case 'U':
  244.                 case 'u':
  245.                     commodup();
  246.                     break;
  247.                 case 'D':
  248.                 case 'd':
  249.                     commoddown();
  250.                     break;
  251.                 case 'T':
  252.                 case 't':
  253.                     ActivateGadget(
  254. commodGadgets[commod_title],commod,NULL);
  255.                     break;
  256.                 case 'F':
  257.                 case 'f':
  258.                     commodfont();
  259.                     break;
  260.                 case 'S':
  261.                 case 's':
  262.                     ActivateGadget(
  263. commodGadgets[commod_pubscreen],commod,NULL);
  264.                     break;
  265.                 default:
  266.                     break;
  267.             }
  268.             break;
  269.         default:
  270.             break;
  271.     }
  272.  
  273.     return (done);
  274. }
  275.  
  276. /*
  277. Function : BOOL processwindowmenucommod(struct IntuiMessage *curr_msg)
  278. Purpose : Processes all menu events (a clone of The Designer routine).
  279. */
  280.  
  281. BOOL processwindowmenucommod(struct IntuiMessage *curr_msg)
  282. {
  283.     struct EasyStruct aboutreq = {
  284.         sizeof(struct EasyStruct),
  285.         0,
  286.         "StickIt2",
  287.         "%s\n\n%s\n\nCX_POPKEY = \"%s\"",
  288.         "Okay"
  289.     };
  290.  
  291.     struct EasyStruct quitreq = {
  292.         sizeof(struct EasyStruct),
  293.         0,
  294.         "StickIt2",
  295.         "Data has changed, continue with quit ?",
  296.         "Okay|Cancel"
  297.     };
  298.  
  299.     struct MenuItem *menuitem;
  300.  
  301.     extern char *versionstring;
  302.  
  303.     UWORD menunumber;
  304.     UWORD menunum;
  305.     UWORD itemnumber;
  306.     UWORD subnumber;
  307.  
  308.     BOOL done = FALSE;
  309.  
  310.     int l;
  311.  
  312.     menunumber = curr_msg->Code;
  313.  
  314.     while ((menunumber != MENUNULL) && (!done)) {
  315.         menuitem = ItemAddress(commodMenu,menunumber);
  316.         menunum = MENUNUM(menunumber);
  317.         itemnumber = ITEMNUM(menunumber);
  318.         subnumber = SUBNUM(menunumber);
  319.  
  320.         switch (menunum) {
  321.             case NOMENU:
  322.                 break;
  323.  
  324.             case commodMenu_project:
  325.                 switch (itemnumber) {
  326.                     case NOITEM:
  327.                         break;
  328.                     case commodMenu_project_save:
  329.                         file_writenotes();
  330.                         break;
  331.                     case commodMenu_project_about:
  332.                         note_blockall();
  333.                         commod_block();
  334.  
  335.                         EasyRequest(commod,&aboutreq,
  336. NULL,&versionstring[6],VERSION_STRING2,prj->prefs.cx_popkey);
  337.                         note_blockclearall();
  338.                         commod_blockclear();
  339.  
  340.                         break;
  341.                     case commodMenu_project_hide:
  342.                         done = commodclose();
  343.                         break;
  344.                     case commodMenu_project_quit:
  345.                         /* Store data */
  346.  
  347.                         if (prj->curr_note)
  348.                             commodtonote(
  349. prj->curr_note);
  350.                         if (prj->projectchanged) {
  351.                             if (EasyRequest(
  352. commod,&quitreq,NULL,TAG_END)) {
  353.                                 done = TRUE;
  354.                                 prj->abouttoquit = TRUE;
  355.                             }
  356.                         }
  357.                         else {
  358.                             done = TRUE;
  359.                             prj->abouttoquit = TRUE;
  360.                         }
  361.  
  362.                         break;
  363.                     default:
  364.                         break;
  365.                 }
  366.                 break;
  367.             case commodMenu_notes:
  368.                 switch(itemnumber) {
  369.                     case NOITEM:
  370.                         break;
  371.                     case commodMenu_notes_new:
  372.                         commodnew();
  373.                         break;
  374.                     case commodMenu_notes_delete:
  375.                         commoddel();
  376.                         break;
  377.                     case commodMenu_notes_redraw:
  378.                         /* Close all */
  379.  
  380.                         for (l = 0; l < NO_NOTES; l++) {
  381.                             if (prj->notes[l])
  382.                                 note_close(
  383. prj->notes[l],FALSE);
  384.                         }
  385.  
  386.                         /* Open all */
  387.  
  388.                         for (l = NO_NOTES -1; l >= 0; l--) {
  389.                             if (prj->notes[l]) {
  390.                                 note_open(
  391. prj->notes[l]);
  392.                                 note_refresh(
  393. prj->notes[l]);
  394.                             }
  395.                         }
  396.                         break;
  397.                     default:
  398.                         break;
  399.                 }
  400.                 break;
  401.             default:
  402.                 break;
  403.         }
  404.  
  405.     menunumber = menuitem->NextSelect;
  406.     }
  407.  
  408.     return(done);
  409. }
  410.  
  411. /*
  412. Function : int openwindowcommod()
  413. Purpose : Calls the Designer's OpenWindowcommod() function then attaches the
  414.     list to the listview gadget.
  415. */
  416.  
  417. int openwindowcommod()
  418. {
  419.     BOOL alreadyopen = FALSE;
  420.  
  421.     int failed;
  422.  
  423.     alreadyopen = commod ? TRUE : FALSE;
  424.  
  425.     failed = OpenWindowcommod(prj->main_msg_port);
  426.  
  427.     /* If the window is open, push its screen to the front */
  428.  
  429.     if (commod)
  430.         ScreenToFront(commod->WScreen);
  431.  
  432.     if ((!failed) && (!alreadyopen)) {
  433.         /* Don't want bloody IDCMP_INTUITICKS */
  434.  
  435.         ModifyIDCMP(commod,commod->IDCMPFlags &= ~IDCMP_INTUITICKS);
  436.  
  437.         GT_SetGadgetAttrs(commodGadgets[commod_listview],commod,NULL,
  438. GTLV_Labels,&prj->commodlist,TAG_END);
  439.     }
  440.  
  441.     return(failed);
  442. }
  443.  
  444. /*
  445. Function : void closewindowcommod()
  446. Purpose : Like Designer's CloseWindowcommod(), but cleans up a few extra things
  447.     as well.
  448. */
  449.  
  450. void closewindowcommod()
  451. {
  452.     /* First detatch list view (not really needed, but neat) */
  453.  
  454.     if (commod)
  455.         GT_SetGadgetAttrs(commodGadgets[commod_listview],commod,NULL,
  456. GTLV_Labels,NULL,TAG_END);
  457.  
  458.     CloseWindowcommod();
  459.  
  460.     prj->curr_note = NULL;
  461. }
  462.  
  463. /*
  464. Function : void processwindowgadcommod(struct IntuiMessage *curr_msg)
  465. Purpose : Handles all the gadget presses in the commodities window.
  466. */
  467.  
  468. void processwindowgadcommod(struct IntuiMessage *curr_msg)
  469. {
  470.     struct Gadget *curr_gad;
  471.  
  472.     curr_gad = (struct Gadget *)curr_msg->IAddress;
  473.  
  474.     switch (curr_gad->GadgetID) {
  475.         case commod_listview:
  476.             commodlistview(curr_msg->Code,curr_msg->Seconds,
  477. curr_msg->Micros);
  478.             break;
  479.         case commod_cyccolour:
  480.             commodcyccolour(curr_msg->Code);
  481.             break;
  482.         case commod_palette:
  483.             commodpalette(curr_msg->Code);
  484.             break;
  485.         case commod_title:
  486.             commodtitle();
  487.             break;
  488.         case commod_font:
  489.             commodfont();
  490.             break;
  491.         case commod_pubscreen:
  492.             commodpubscreen();
  493.             break;
  494.         case commod_new:
  495.             commodnew();
  496.             break;
  497.         case commod_del:
  498.             commoddel();
  499.             break;
  500.         case commod_up:
  501.             commodup();
  502.             break;
  503.         case commod_down:
  504.             commoddown();
  505.             break;
  506.         default:
  507.             break;
  508.     }
  509. }
  510.  
  511. /*
  512. Function : void commod_block()
  513. Purpose : Blocks the current note to mouse input and puts up a wait pointer.
  514. */
  515.  
  516. void commod_block()
  517. {
  518.     /* Only do it if window open */
  519.  
  520.     if (!commod)
  521.         return;
  522.  
  523.     InitRequester(&prj->blockreq);
  524.  
  525.     if (Request(&prj->blockreq,commod))
  526.         SetPointer(commod,WaitPointer,16,16,-6,0);
  527. }
  528.  
  529. /*
  530. Function : void commod_blockclear()
  531. Purpose : Clears the commodities win for mouse input and returns to the normal
  532.     pointer.
  533. */
  534.  
  535. void commod_blockclear()
  536. {
  537.     /* Only do it if window open */
  538.  
  539.     if (!commod)
  540.         return;
  541.  
  542.     ClearPointer(commod);
  543.     EndRequest(&prj->blockreq,commod);
  544. }
  545.  
  546.